package com.ergou.easylive.admin.controller.system;

import com.ergou.easylive.admin.annotation.AccessLog;
import com.ergou.easylive.admin.entity.dto.SysPermissionCreateDTO;
import com.ergou.easylive.admin.entity.dto.SysPermissionQueryDTO;
import com.ergou.easylive.admin.entity.dto.SysPermissionUpdateDTO;
import com.ergou.easylive.admin.entity.po.SysPermissions;
import com.ergou.easylive.admin.entity.vo.menu.MenuVO;
import com.ergou.easylive.admin.exception.AuthException;
import com.ergou.easylive.admin.service.SysPremissService;
import com.ergou.easylive.common.entity.response.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;

/**
 * 权限控制器
 *
 * @author Er-Gou
 * @date 2025-10-03 00:33
 */
@RestController
@RequestMapping("/premiss")
@Slf4j
@Tag(name = "权限管理", description = "权限管理相关接口")
@Validated
public class SysPremissController {

    @Resource
    private SysPremissService sysPremissService;

    @GetMapping("/menu")
    @Operation(summary = "获取菜单", description = "获取系统的菜单树结构")
    @AccessLog(value = "获取菜单")
    public Result<List<MenuVO>> getMenu() {
        log.info("开始获取菜单树");
        try {
            List<MenuVO> menuTree = sysPremissService.getMenuTree();
            log.info("获取菜单树成功，共{}个菜单项", menuTree.size());
            // 测试
            return Result.success(menuTree);
        } catch (Exception e) {
            log.error("获取菜单树失败", e);
            return Result.fail("获取菜单失败");
        }
    }

    @GetMapping("/list")
    @Operation(summary = "获取权限列表", description = "获取权限列表")
    @AccessLog(value = "获取权限列表")
    public Result<LinkedList<SysPermissions>> getPermissions() {
        try {
            LinkedList<SysPermissions> permissionList = sysPremissService.getPremissionList();
            return Result.success(permissionList);
        } catch (AuthException e) {
            throw new AuthException(e.getCustomCode(), e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询权限", description = "根据权限ID查询权限详情")
    @AccessLog(value = "查询权限详情")
    public Result<SysPermissions> getById(
            @Parameter(description = "权限ID", required = true) @PathVariable Integer id) {
        try {
            SysPermissions permission = sysPremissService.getById(id);
            if (permission == null) {
                return Result.fail("权限不存在");
            }
            return Result.success(permission);
        } catch (Exception e) {
            log.error("查询权限详情失败，权限ID：{}", id, e);
            return Result.fail("查询权限详情失败");
        }
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询权限列表", description = "根据条件分页查询权限列表")
    @AccessLog(value = "分页查询权限列表")
    public Result<Object> getPageList(@Valid @RequestBody SysPermissionQueryDTO queryDTO) {
        try {
            return sysPremissService.getPageList(queryDTO);
        } catch (Exception e) {
            log.error("分页查询权限列表失败", e);
            return Result.fail("查询失败");
        }
    }

    @PostMapping
    @Operation(summary = "新增权限", description = "创建新的权限")
    @AccessLog(value = "新增权限")
    public Result<Object> create(@Valid @RequestBody SysPermissionCreateDTO createDTO) {
        try {
            return sysPremissService.create(createDTO);
        } catch (Exception e) {
            log.error("新增权限失败", e);
            return Result.fail("新增权限失败");
        }
    }

    @PutMapping
    @Operation(summary = "更新权限", description = "更新权限信息")
    @AccessLog(value = "更新权限")
    public Result<Object> update(@Valid @RequestBody SysPermissionUpdateDTO updateDTO) {
        try {
            return sysPremissService.update(updateDTO);
        } catch (Exception e) {
            log.error("更新权限失败", e);
            return Result.fail("更新权限失败");
        }
    }

    @PostMapping("/{id}")
    @Operation(summary = "删除权限", description = "根据ID删除权限")
    @AccessLog(value = "删除权限")
    public Result<Object> deleteById(
            @Parameter(description = "权限ID", required = true) @PathVariable Integer id) {
        try {
            return sysPremissService.deleteById(id);
        } catch (Exception e) {
            log.error("删除权限失败，权限ID：{}", id, e);
            return Result.fail("删除权限失败");
        }
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除权限", description = "批量删除权限")
    @AccessLog(value = "批量删除权限")
    public Result<Object> deleteByIds(@RequestBody List<Integer> ids) {
        try {
            return sysPremissService.deleteByIds(ids);
        } catch (Exception e) {
            log.error("批量删除权限失败", e);
            return Result.fail("批量删除权限失败");
        }
    }

    @GetMapping("/parent/{parentId}")
    @Operation(summary = "根据父级ID查询子权限", description = "根据父级权限ID查询子权限列表")
    @AccessLog(value = "查询子权限列表")
    public Result<List<SysPermissions>> getByParentId(
            @Parameter(description = "父级权限ID", required = true) @PathVariable Integer parentId) {
        try {
            List<SysPermissions> children = sysPremissService.getByParentId(parentId);
            return Result.success(children);
        } catch (Exception e) {
            log.error("查询子权限列表失败，父级ID：{}", parentId, e);
            return Result.fail("查询子权限列表失败");
        }
    }
}
